package com.example.campus_activity_backend.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.campus_activity_backend.common.ErrorCode;
import com.example.campus_activity_backend.domain.Student;
import com.example.campus_activity_backend.exception.BusinessException;
import com.example.campus_activity_backend.service.StudentService;
import com.example.campus_activity_backend.mapper.StudentMapper;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author Hongxia
* @description 针对表【t_manager_student】的数据库操作Service实现
* @createDate 2023-11-17 14:35:16
*/
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
    implements StudentService {

    @Override
    public long addStudent(Student student) {
        //参数校验
        if(student == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String stuName = student.getStuName();
        if(stuName == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"学生姓名为空");
        }
        String currentGrade = student.getCurrentGrade();
        if(currentGrade == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"当前年级为空");
        }
        String fromPlace = student.getFromPlace();
        if(fromPlace == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"来源地址为空");
        }
        String isFirst = student.getIsFirst();
        if(isFirst == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"是否首次参加本次活动为空");
        }
        Integer loginId = student.getLoginId();
        if(loginId == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"关联登录id为空");
        }
        String parentPhone = student.getParentPhone();
        if(parentPhone == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"监护人电话为空");
        }
        //添加数据库
        boolean saveResult = this.save(student);
        if(!saveResult){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"学员添加失败");
        }
        return student.getId();
    }

    @Override
    public boolean auditStudent(Integer studentId, Integer status) {
        //校验参数
        if(studentId == null || status == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Student student = this.getById(studentId);
        if(student == null){
            throw new BusinessException(ErrorCode.NO_FOUND_ERROR,"学生不存在");
        }
        //更新数据库
        boolean result = false;
        if(status == 1){
            result = this.update().set("comment" , "已审核").eq("id", studentId).update();
        }else if(status == 0){
            result = this.update().set("comment = ","未通过").eq("id",studentId).update();
        }else{
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"状态参数错误");
        }
        return result;
    }

    @Override
    public boolean batchAuditStudent(List<Integer> idList, Integer status) {
        //校验参数
        if(idList == null || status == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        List<Student> students = this.listByIds(idList);
        //更新数据库
        boolean result = false;
        for(Student student : students){
            if(status == 1){
                result = this.update().set("comment" , "已审核").eq("id", student.getId()).update();
            }else if(status == 0){
                result = this.update().set("comment" , "未通过").eq("id", student.getId()).update();
            }else{
                throw new BusinessException(ErrorCode.PARAMS_ERROR,"状态参数错误");
            }
        }
        return result;
    }


}




